#ifndef Algebra_MpiEnvironment_h
#define Algebra_MpiEnvironment_h

#include "AlgebraModule.h"
#include <string>

SG_NAMESPACE_OPEN
namespace Algebra
{
    /**
     * @brief Setup the MPI environment.
     *
     * A MPI program consists of many cooperating processes (possibly running on different computers)
     * that communicate among themselves by passing messages. The first step in a Boost.MPI is to create
     * this object that initializes the MPI environment and enables communication among the processes.
     *
     * @see https://www.boost.org/doc/libs/1_85_0/doc/html/mpi/tutorial.html
     *
     * This class initialize MPI on construction and finalize it on destruction. If one has been created
     * or destroyed, however, it will do nothing on either construction or destruction.
     *
     * @author nene
     * @date December, 2024.
     */
    class ALGEBRA_EXPORT MpiEnvironment
    {
    public:
        MpiEnvironment (int argc, char **argv);
        virtual ~MpiEnvironment ();

        /// Return true if MPI has been initialized.
        virtual bool initialized ();

        /// Return true if MPI has been finalized.
        virtual bool finalized ();

        /// The rank of the in the MPI_COMM_WORLD communicator.
        virtual int world_rank ();

        /// The number of processes in the MPI_COMM_WORLD communicator.
        virtual int world_size ();

        /// Get the name of the processor.
        virtual std::string processor_name ();

        /// Return MPI version.
        virtual std::string version ();

        /// Terminate MPI execution environment.
        static void abort (int errcode);

    private:
        /// Whether this environment object called MPI_Init
        bool mInitialized = false;
    };
}  // namespace Algebra

SG_NAMESPACE_CLOSE

#endif  // Algebra_MpiEnvironment_h